package dao

import (
	__ "activity_srv/basic/proto"
	"activity_srv/handler/models"
	"errors"
)

// SetFaceId 设置用户面容ID信息
func SetFaceId(req *__.SetFaceIdReq) error {
	// 查询用户是否已经设置过面容ID
	var faceId models.FaceId
	faceId.UserId = uint32(req.UserId)
	if err := faceId.GetByUserId(); err == nil {
		// 如果已存在，则更新
		faceId.FaceToken = req.FaceToken
		faceId.FaceData = req.FaceData
		faceId.Status = uint8(req.Status)
		return faceId.UpdateFaceId()
	}
	
	// 如果不存在，则创建新记录
	newFaceId := models.FaceId{
		UserId:    uint32(req.UserId),
		FaceToken: req.FaceToken,
		FaceData:  req.FaceData,
		Status:    uint8(req.Status),
	}
	return newFaceId.CreateFaceId()
}

// GetFaceId 获取用户面容ID信息
func GetFaceId(req *__.GetFaceIdReq) (*models.FaceId, error) {
	var faceId models.FaceId
	faceId.UserId = uint32(req.UserId)
	if err := faceId.GetByUserId(); err != nil {
		return nil, errors.New("未设置面容ID")
	}
	return &faceId, nil
}

// UpdateFaceIdStatus 更新面容ID状态
func UpdateFaceIdStatus(req *__.UpdateFaceIdStatusReq) error {
	var faceId models.FaceId
	faceId.UserId = uint32(req.UserId)
	if err := faceId.GetByUserId(); err != nil {
		return errors.New("未设置面容ID")
	}
	faceId.Status = uint8(req.Status)
	return faceId.UpdateFaceId()
}

// DeleteFaceId 删除用户面容ID信息
func DeleteFaceId(req *__.DeleteFaceIdReq) error {
	var faceId models.FaceId
	faceId.UserId = uint32(req.UserId)
	if err := faceId.GetByUserId(); err != nil {
		return errors.New("未设置面容ID")
	}
	return faceId.DeleteFaceId()
}

// VerifyFaceId 验证面容ID
func VerifyFaceId(req *__.VerifyFaceIdReq) (bool, error) {
	var faceId models.FaceId
	faceId.UserId = uint32(req.UserId)
	if err := faceId.GetByUserId(); err != nil {
		return false, errors.New("未设置面容ID")
	}
	
	// 检查面容ID状态是否启用
	if faceId.Status != 1 {
		return false, errors.New("面容ID未启用")
	}
	
	// 这里应该有实际的面容验证逻辑
	// 为了示例，我们只比较传入的token是否与存储的一致
	// 在实际应用中，这里应该调用真实的面容识别服务进行验证
	if req.FaceToken == faceId.FaceToken {
		return true, nil
	}
	
	return false, errors.New("面容验证失败")
}